package com.witdata.controller.community;

import com.witdata.common.domain.Result;
import com.witdata.common.domain.Status;
import com.witdata.common.domain.dto.ArticleCommentDto;
import com.witdata.common.domain.dto.ArticleCommentListDto;
import com.witdata.common.domain.dto.ArticleSubCommentDto;
import com.witdata.common.exception.GlobalException;
import com.witdata.common.service.community.ArticleCommentService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author LiuQi
 */
@RestController
@RequestMapping("/community/article/comment")
@RequiredArgsConstructor
public class ArticleCommentController {
    private final ArticleCommentService articleCommentService;

    /**
     * 获取文章评论列表
     */
    @GetMapping("/list/{articleId}")
    public Result<ArticleCommentListDto> getArticleCommentList(@PathVariable("articleId") Long articleId) {
        ArticleCommentListDto articleCommentList =  articleCommentService.getArticleCommentList(articleId);
        return Result.ok(Status.SUCCESS, articleCommentList);
    }

    /**
     * 发表评论
     */
    @PostMapping("/publish")
    public Result<String> publishArticleComment(@RequestBody ArticleCommentDto articleCommentDto) {
        if (ObjectUtils.isEmpty(articleCommentDto) || ObjectUtils.isEmpty(articleCommentDto.getArticleId())||
                ObjectUtils.isEmpty(articleCommentDto.getContent())) {
            throw new GlobalException(Status.PARAMERROR);
        }
        articleCommentService.publishArticleComment(articleCommentDto);
        return Result.ok(Status.PublishSuccess);
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/delete")
    public Result<String> deleteArticleComment(@RequestParam("commentId") Long commentId) {
        articleCommentService.deleteArticleComment(commentId);
        return Result.ok(Status.DELETESUCCESS);
    }

    /**
     * 发表二级评论
     */
    @PostMapping("/publish/second")
    public Result<String> publishSecondArticleComment(@RequestBody ArticleSubCommentDto articleCommentDto) {
        if (ObjectUtils.isEmpty(articleCommentDto) || ObjectUtils.isEmpty(articleCommentDto.getArticleId())||
                ObjectUtils.isEmpty(articleCommentDto.getContent()) || ObjectUtils.isEmpty(articleCommentDto.getParentCommentId())) {
            throw new GlobalException(Status.PARAMERROR);
        }
        articleCommentService.publishSecondArticleComment(articleCommentDto);
        return Result.ok(Status.PublishSuccess);
    }

    /**
     * 删除二级评论
     */
    @DeleteMapping("/delete/second")
    public Result<String> deleteSecondArticleComment(@RequestParam("commentId") Long commentId) {
        articleCommentService.deleteSecondArticleComment(commentId);
        return Result.ok(Status.DELETESUCCESS);
    }
}
